<?php
namespace PsiFramework\Applications\AlloMediaUser\Classes\Actions;

import("Applications.AlloMediaUser.Classes.Actions.ShopAction");
import("Classes.Util.HTTP.AuthenticationManager");
import("Classes.Factory.DAOFactory");
import("Plugins.Shop.Classes.Model.Order");

use \PsiFramework\Applications\AlloMediaUser\Classes\Actions\ShopAction;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\InformationPart;
use \PsiFramework\Classes\Util\HTTP\AuthenticationManager;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Classes\Util\Object\ReferenceObject;
use \PsiFramework\Classes\Util\Parameters\Action\ActionParameters;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Plugins\Shop\Classes\Model\Order;
use \PsiFramework\Plugins\Shop\Classes\Model\OrderItem;
use \PsiFramework\Plugins\Shop\Classes\Model\OrderItemLink;
use \PsiFramework\Plugins\Shop\Classes\Model\ItemCategory;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de l'index
 *
 * @package Actions.Pages
 */
class AddItemAction extends ShopAction
{
    
    public function __construct(ActionParameters $actionParameters)
    {
        parent::__construct($actionParameters);
    }

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     *
     */
    public function execute()
    {
        parent::execute();

        $formId = $this->_request->getParameter('form_id');
        $form = null ;
        
        if (!isNullOrEmptyString($formId)) {
        	$formDao = DAOFactory::getDAO('Form');
        	$form = $formDao->retrieveByPk($formId);
        }
        
        $this->_response->setAttribute('form', $form);
        $entry = $this->_response->getAttribute('entry');
        
        $responseIdDao = DAOFactory::getDAO('ResponseId');
        $responseId = $responseIdDao->retrieveByEntryAndTheme($entry, $form);
        
        $this->_response->setAttribute('responseId', $responseId);
        
        $samplePart = new InformationPart($entry->getCategory()->getProfile(), $form);
        $informationPartDao = DAOFactory::getDAO('InformationPart');
        $part = $informationPartDao->retrieveByPk($samplePart->getId());
        
        $this->_response->setAttribute('part', $part);
        
        $refObject = new ReferenceObject('ResponseId', $responseId->getId());
        
        $this->_response->setAttribute('item', self::getItem($refObject, $part));
    }
}
